package com.car.account.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.car.account.controller.form.storesForm.*;
import com.car.account.dto.storesDto.ClerkDto;
import com.car.account.dto.storesDto.SelectClerkDto;
import com.car.account.dto.storesDto.StoresDto;
import com.car.account.entity.Stores;
import com.car.account.exception.AccountException;
import com.car.account.param.storesParam.InsertClerkParam;
import com.car.account.param.storesParam.SelectClerkDtoParam;
import com.car.account.param.storesParam.UpdateClerkRoleParam;
import com.car.account.service.StoresService;
import com.car.commons.core.model.Result;
import com.car.commons.core.util.JwtUtil;
import com.car.commons.web.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 董旭
 * @since 2024-08-28
 */
@RestController
@RequestMapping("/stores")
@Api(tags = "门店账号")
public class StoresController {

    @Resource
    private StoresService storesService;

    @PostMapping("/storesPassLogin")
    @ApiOperation(value = "门店账号密码登录")
    public Result storesPassLogin(@RequestBody StoresPassLoginForm form) throws Exception{
        Integer clerkId = storesService.storesPassLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("clerkId",clerkId);
        String token = JwtUtil.createClerkToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("门店账号登录成功");
    }

    @GetMapping("/sendStoresLoginCode")
    @ApiOperation(value = "发送门店账号登录验证码")
    public Result sendStoresLoginCode(String clerkPhone) throws Exception{
        storesService.sendStoresLoginCode(clerkPhone);
        return Result.getResult().setMsg("登录短信验证码发送成功");
    }

    @PostMapping("/storesCodeLogin")
    @ApiOperation(value = "门店账号验证码登录")
    public Result storesCodeLogin(@RequestBody StoresCodeLoginForm form) throws Exception{
        Integer clerkId = storesService.storesCodeLogin(form);
        Map<String, Object> map = new HashMap<>();
        map.put("clerkId",clerkId);
        String token = JwtUtil.createClerkToken(map, 24 * 60);
        WebUtil.getResponse().setHeader("car-token",token);
        WebUtil.getResponse().setHeader("Access-Control-Expose-Headers","car-token");
        return Result.getResult().setMsg("门店账号登录成功");
    }

    @GetMapping("/selectClerkDto.md")
    @ApiOperation(value = "店员查询自己信息")
    public Result<ClerkDto> selectClerkDto() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        ClerkDto clerkDto = storesService.selectClerkDto(clerkId);
        return Result.getResult(ClerkDto.class).setData(clerkDto).setMsg("查询当前店员信息成功");
    }

    @PostMapping("/selectClerks.md")
    @ApiOperation(value = "店长根据姓名，手机号，状态模糊查询店员信息")
    public Result<Page> selectClerks(@RequestBody SelectClerkDtoForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        SelectClerkDtoParam param = BeanUtil.toBean(form, SelectClerkDtoParam.class);
        param.setCurrentClerkId(currentClerkId);
        Page<SelectClerkDto> page = storesService.selectClerks(param);
        return Result.getResult(Page.class).setData(page).setMsg("店长查询店员信息成功");
    }

    @GetMapping("/disableClerk.md")
    @ApiOperation(value = "店长禁用店员")
    public Result disableClerk(Integer clerkId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        storesService.disableClerk(currentClerkId, clerkId);
        return Result.getResult().setMsg("禁用店员成功");
    }

    @GetMapping("/enableClerk.md")
    @ApiOperation(value = "店长解禁店员")
    public Result enableClerk(Integer clerkId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        storesService.enableClerk(currentClerkId, clerkId);
        return Result.getResult().setMsg("解禁店员成功");
    }

    @GetMapping("/clerkUpdatePass.md")
    @ApiOperation(value = "店员修改密码")
    public Result clerkUpdatePass(String clerkPass) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        storesService.clerkUpdatePass(clerkId, clerkPass);
        return Result.getResult().setMsg("修改密码成功");
    }

    @PostMapping("/clerkUpdateImage.md")
    @ApiOperation(value = "店员更改头像")
    public Result<String> clerkUpdateImage(MultipartFile mf) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        String imageName = storesService.clerkUpdateImage(mf, clerkId);
        return Result.getResult(String.class).setData(imageName).setMsg("更换头像成功");
    }

    @GetMapping("/clerkUpdatePhone.md")
    @ApiOperation(value = "店员修改绑定手机号")
    public Result clerkUpdatePhone(String clerkPhone) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        storesService.clerkUpdatePhone(clerkId, clerkPhone);
        return Result.getResult().setMsg("修改绑定手机号成功");
    }

    @PostMapping("/insertClerk.md")
    @ApiOperation(value = "店长添加店员")
    public Result insertClerk(@RequestBody InsertClerkForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        InsertClerkParam param = BeanUtil.toBean(form, InsertClerkParam.class);
        param.setCurrentClerkId(currentClerkId);
        storesService.insertClerk(param);
        return Result.getResult().setMsg("店长添加店员成功");
    }

    @GetMapping("/selectStoresRole.md")
    @ApiOperation(value = "店长查询当前店员身份id")
    public Result<Integer> selectStoresRole(Integer clerkId) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        Integer roleId = storesService.selectStoresRole(currentClerkId, clerkId);
        return Result.getResult(Integer.class).setData(roleId).setMsg("店长查询当前店员身份id成功");
    }

    @PostMapping("/updateClerkRole.md")
    @ApiOperation(value = "店长根据身份id修改店员角色")
    public Result updateClerkRole(@RequestBody UpdateClerkRoleForm form) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        UpdateClerkRoleParam param = BeanUtil.toBean(form, UpdateClerkRoleParam.class);
        param.setCurrentClerkId(currentClerkId);
        storesService.updateClerkRole(param);
        return Result.getResult().setMsg("店长根据身份id修改店员角色成功");
    }

    /**
     * openfeign
     */
    @GetMapping("/judgmentClerkState.ty")
    @ApiOperation(value = "判断店员或店长的状态是否正常")
    public Result<Boolean> judgmentClerkState() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        //判断是否存在店员账号
        Stores currentClerk = storesService.getById(currentClerkId);
        if (currentClerk == null || currentClerk.getClerkState() == 2)
            throw new AccountException(3001,"店员账号不存在");
        //判断当前门店账号状态
        if (currentClerk.getClerkState() == 1) throw new AccountException(3002,"店员账号已被禁用");
        return Result.getResult(Boolean.class).setData(true).setMsg("状态正常");
    }

    /**
     * openfeign
     */
    @GetMapping("/ById.ty")
    @ApiOperation(value = "通过店员id返回一个店员dto")
    public Result<StoresDto> findByIdStores(@RequestParam("clerkId") Integer clerkId) throws Exception{
        Stores stores = storesService.getById(clerkId);
        if (stores == null) throw new AccountException(2000,"不存在该店员");
        StoresDto storesDto = BeanUtil.toBean(stores, StoresDto.class);
        return Result.getResult(StoresDto.class).setData(storesDto).setMsg("通过id查询店员信息成功");
    }

    /**
     * openfeign
     */
    @GetMapping("/findsClerksByIds.ty")
    @ApiOperation(value = "通过店员id集合返回店员dto集合")
    public Result<List<StoresDto>> findsClerksByIds(@RequestParam("clerkIds") Set<Integer> clerkIds) throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer currentClerkId = JwtUtil.getClerkId(token);
        List<StoresDto> storesDtos = storesService.findsClerksByIds(currentClerkId, clerkIds);
        return Result.getListResult(StoresDto.class).setData(storesDtos).setMsg("通过店员id集合返回店员dto集合成功");
    }


    /**
     * openfeign
     */
    @GetMapping("/findStoresIdByClerkId.ty")
    @ApiOperation(value = "通过店员id返回门店id")
    public Result<Integer> findStoresIdByClerkId() throws Exception{
        String token = WebUtil.getRequest().getHeader("car-token");
        Integer clerkId = JwtUtil.getClerkId(token);
        Integer storesId = storesService.findStoresIdByClerkId(clerkId);
        return Result.getResult(Integer.class).setData(storesId).setMsg("通过店员id返回门店id成功");
    }

}

